/**
 * @file lowbattery.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_LOWBATTERY != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void lowbattery_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * lowbattery_create(lv_obj_t * activity_obj);
static void lowbattery_destory(void);

/**********************
 *  STATIC VARIABLES
**********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_res_t lowbattery_create_btn_action(lv_obj_t * btn)
{
    (void)btn;

    lv_watch_go_home();
    lv_watch_png_cache_all_free();

    lv_obj_t * obj = lowbattery_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return LV_RES_OK;

    return LV_RES_OK;
}

void lowbattery_task(lv_task_t * task)
{
    if(HAL_CHG_CONNECTED == Hal_Charger_Get_Status()) {
        if(NULL != lv_watch_get_activity_obj(ACT_ID_LOWBATTERY)) {
            // exit low battery mode
            printf("power charging start and exit low battery mode\n");
            lowbattery_destory();
        }
    } else {
        if(Hal_Battery_Get_Status() < ENTER_LOWBATTERY_MODE_LEVEL) {
            if((NULL == lv_watch_get_activity_obj(ACT_ID_LOWBATTERY))
                    && (NULL == lv_watch_get_activity_obj(ACT_ID_PHONE))) {
                // enter low battery mode
                printf("enter low battery mode\n");
                lowbattery_create_btn_action(NULL);
#if USE_LV_WATCH_AUDIO_PLAYER != 0
                audio_player_lowbattery_process();
#endif
            } else {
                // update time
                if(NULL == lv_watch_get_activity_obj(ACT_ID_PHONE)) {
                    lv_lowbattery_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_LOWBATTERY));
                    hal_rtc_t                  tm;
                    Hal_Rtc_Gettime(&tm);// get time
                    char * rtctime = lv_mem_alloc(6);
                    memset(rtctime, 0, 6);
                    snprintf(rtctime, 6, "%02d:%02d", tm.tm_hour, tm.tm_min);
                    lv_label_set_text(ext->timelabel, rtctime); //refresh the label
                    lv_mem_free(rtctime);
                }
                if(Hal_Battery_Get_Status() == AUTO_POWER_OFF_LEVEL) {
                    printf("power off in low battery mode\n");
                    shutdown_confirm_btn_action(NULL, LV_EVENT_CLICKED);
                }
            }
        } else {
            // exit low battery mode
            if(NULL != lv_watch_get_activity_obj(ACT_ID_LOWBATTERY)) {
                lowbattery_destory();
            }
        }
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_obj_t * lowbattery_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_LOWBATTERY;
        activity_ext.create = lowbattery_create;
        activity_ext.prepare_destory = lowbattery_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * content = lv_cont_create(activity_obj, NULL);
    LV_ASSERT_MEM(content);
    if(content == NULL) return NULL;
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_style_local_bg_color(content,LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_BLACK);
    lv_obj_set_style_local_bg_opa(content,LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,LV_OPA_COVER);
    lv_obj_set_size(content,  LV_HOR_RES, LV_VER_RES);
    lv_obj_set_drag(content, false);
    lv_cont_set_layout(content, LV_LAYOUT_COLUMN_MID);

    lv_obj_t * label = lv_label_create(content, NULL);
    lv_obj_set_base_dir(label, LV_BIDI_DIR_LTR);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20_gray);
    hal_rtc_t                  tm;
    Hal_Rtc_Gettime(&tm); // get time
    char * rtctime = lv_mem_alloc(6);
    memset(rtctime, 0, 6);
    snprintf(rtctime, 6, "%02d:%02d", tm.tm_hour, tm.tm_min);
    lv_label_set_text(label, rtctime);
    lv_mem_free(rtctime);
    lv_lowbattery_ext_t * ext =
        lv_obj_allocate_ext_attr(activity_obj, sizeof(lv_lowbattery_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    ext->timelabel = label;// store the label obj

    lv_obj_t * img = lv_img_create(content, NULL);
    lv_img_set_src(img, ICON_LOWBATTERY_CHARGE);
    lv_obj_set_size(img, 52, 83);

    label = lv_label_create(content, NULL);
    lv_obj_set_base_dir(label, LV_BIDI_DIR_LTR);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_size(label, 200, 45);
    lv_label_set_text_id(label, WATCH_TEXT_ID_LOWBATTERY);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);

    lv_obj_t * btn;
    btn = lv_imgbtn_create(content, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_LOWBATTERY_DIAL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_LOWBATTERY_DIAL);
    lv_obj_set_event_cb(btn, phonebook_create_btn_event_cb);

    return activity_obj;
}

static void lowbattery_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
    lv_lowbattery_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_LOWBATTERY));
    ext->timelabel = NULL;
}

static void lowbattery_destory(void)
{
    lowbattery_prepare_destory(NULL);
    lv_obj_del(lv_watch_get_activity_obj(ACT_ID_LOWBATTERY));
}
#endif /*USE_LV_WATCH_LOWBATTERY*/
